//
// Created by ktsq on 2021/8/7.
//

#include "camera.h"

#include "../core/transform.h"

namespace csugl {

    Camera::Camera(float aspect, float near, float far)
        : aspect(aspect), near(near), far(far) {}

    glm::mat4 Camera::get_view_mat(const Transform &transform) const
    {
        // glm::vec3 front = glm::rotate( transform.rotation, { 0, 0,   -1.f  } );
        // glm::vec3 up    = glm::rotate( transform.rotation, { 0, 1.f, 0     } );
        return glm::lookAt(
            transform.position,
            transform.position + glm::rotate(transform.rotation, {0, 0, -1.f}),
            glm::rotate(transform.rotation, {0, 1.f, 0})
        );
    }

    OrthographicCamera::OrthographicCamera(float aspect, float near, float far, float zoom)
            : Camera(aspect, near, far), zoom(zoom) {}

    glm::mat4 OrthographicCamera::get_projection_mat() const
    {
        float half_zoom        = zoom * .5f;
        float half_zoom_aspect = half_zoom / aspect;
        return glm::ortho(-half_zoom, half_zoom, -half_zoom_aspect, half_zoom_aspect, near, far);
    }

    PerspectiveCamera::PerspectiveCamera(float aspect, float near, float far, float fov)
            : Camera(aspect, near, far), fov(fov){}

    glm::mat4 PerspectiveCamera::get_projection_mat() const
    {
        return glm::perspective(fov, aspect, near, far);
    }
}